from xml.sax.handler import ContentHandler
from xml.sax import parse
from xml.dom.minidom import parseString
from random import Random
from changefile import HeadlineHandler
import os, re, string, sys
from create import menuitem

def change(file, c):
    headlines = []
    if(c != 0):
        file = os.getcwd()+'\\'+file
        parse(file, HeadlineHandler(headlines))
        h = HeadlineHandler(headlines)
        headlines = h.getheadlines()
    else:
        headlines.append("")
        headlines.append("")
        headlines.append("")
        headlines.append("")
    newfile = changexml(headlines, file)
    return newfile
        
def changexml(headlines, file):
    change = 0
    new = 0
    if (headlines[0] == ""):
        new = 1
        cmd = 'y'
    else:
        print 'The current title is: ' + headlines[0]
        cmd = raw_input('Would you like to change the title? y or n ')
    if(cmd.strip() == 'y'):
        titlecmd = raw_input('What would you like the title to be? ')
        headlines[0] = titlecmd
        change = 1
        
    if (headlines[1] == ""):
        cmd = 'y'
    else:
        print 'The current heading is: ' + headlines[1]
        cmd = raw_input('Would you like to change the heading? y or n ')
    if(cmd.strip() == 'y'):
        headingcmd = raw_input('What would you like the heading to be? ')
        headlines[1] = headingcmd
        change = 1
    
    
    if (headlines[2] == ""):
        cmd = 'y'
    else:
        print 'The current sub-heading is: ' + headlines[2]
        cmd = raw_input('Would you like to change the sub-heading ? y or n ')
    if(cmd.strip() == 'y'):
        subheadcmd = raw_input('What would you like the sub-heading to be? ')
        headlines[2] = subheadcmd
        change = 1
    
    
    if (headlines[3] == ""):
        cmd = 'y'
    else:
        print 'The current content is: ' + headlines[3]
        cmd = raw_input('Would you like to change the content? y or n ')
    if(cmd.strip() == 'y'):
        contentcmd = raw_input('What would you like the content to be? ')
        headlines[3] = contentcmd
        change = 1
        
    if(change == 1):
        #START CREATION OF DOCUMENT WITH DOM
        # create a new document
        doc = parseString(u'<page/>'.encode('UTF-8'))
        
        pagedata = doc.documentElement
        # create a sect1 element
        s1 = doc.createElementNS(None,u'pagedata')
        # add it under the root element
        pagedata.appendChild(s1)
        # create a title element with a text node inside
        s1.appendChild(doc.createElementNS(None,u'title'))
        title = doc.createTextNode(headlines[0])
        s1.firstChild.appendChild(title)
        
        heading = doc.documentElement
        # create a sect1 element
        s2 = doc.createElementNS(None,u'heading')
        # add it under the root element
        heading.appendChild(s2)
        txt = doc.createTextNode(headlines[1])
        s2.appendChild(txt)
        
        subhead = doc.documentElement
        # create a sect1 element
        s3 = doc.createElementNS(None,'subhead')
        # add it under the root element
        subhead.appendChild(s3)
        txt = doc.createTextNode(headlines[2])
        s3.appendChild(txt)
        
        content = doc.documentElement
        # create a sect1 element
        s4 = doc.createElementNS(None,'content')
        # add it under the root element
        content.appendChild(s4)
        # create a title element with a text node inside
        s4.appendChild(doc.createElementNS(None,u'span'))
        txt = doc.createTextNode(headlines[3])
        s4.firstChild.appendChild(txt)
        
        # write the result
        s = doc.toprettyxml()
        if(new == 1):
            menuitem(file)
        f = open(file, 'w')
        f.write(s)
        f.close()
        print 'The following are your requested changes: '
        print 'Title: ' + headlines[0]
        print 'Heading: ' + headlines[1]
        print 'SubHeading: ' + headlines[2]
        print 'Content: ' + headlines[3]
        return file
    else:
        return 0
